package com.mike.bdb.ui.provider.table;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mike.bdb.ui.SecondaryEntityTable;
import com.mike.bdb.util.DataCurrent;
import com.mike.bdb.util.ImageManager;
import com.mike.bdb.util.ReflectUtil;

public class MikeTableLabelProvider implements ITableLabelProvider {

	private static final Logger LOG = LoggerFactory.getLogger(MikeTableLabelProvider.class);
	
	private TableViewer tableViewer;
	private SecondaryEntityTable secondaryEntityTable;
	public static Map<String, Button> tableButtonMap = new HashMap<String, Button>();
	
	public MikeTableLabelProvider(TableViewer tableViewer, SecondaryEntityTable secondaryEntityTable) {
		this.tableViewer = tableViewer;
		this.secondaryEntityTable = secondaryEntityTable;
	}
	
	@Override
	public Image getColumnImage(Object obj, int index) {
		if(index == 0) return ImageManager.getImage(ImageManager.ICON_BULLET_KEY);
		String curStoreClazz = DataCurrent.getInstance().curStoreClazz;
		boolean isSecondaryField = ReflectUtil.isSecondaryField(curStoreClazz, index, obj);
		if(isSecondaryField) {
			Table table = tableViewer.getTable();
			
			TableItem[] items = table.getItems();
			for (int i = 0; i < items.length; i++) {
				if (!tableButtonMap.containsKey(i + "-" + index)) {
					TableEditor tableEditor = new TableEditor(table);
					
					Button button = new Button(table, SWT.PUSH);
					button.setImage(ImageManager.getImage(ImageManager.ICON_BULLET_KEY2));
					
					//事件
					addListenerFor(button, index);
					
					button.pack();
					tableEditor.minimumWidth = button.getSize().x;
					tableEditor.horizontalAlignment = SWT.RIGHT;
					tableEditor.setEditor(button, items[i], index);
					tableButtonMap.put(i + "-" + index, button);
				}
			}
			return null;
		}
		return null;
	}
	
	@Override
	public String getColumnText(Object obj, int index) {
		
		String curStoreClazz = DataCurrent.getInstance().curStoreClazz;
		
		return ReflectUtil.invokeGet(curStoreClazz, index, obj);
	}

	private void addListenerFor(Control control, final int index) {
		if(null == control) return;
		control.addMouseListener(new MouseAdapter(){

			@Override
			public void mouseUp(MouseEvent e) {
				Point p = Display.getDefault().getCursorLocation();
				p = tableViewer.getTable().toControl(p);
				TableItem item = tableViewer.getTable().getItem(p);
				if(null == item) return;
				tableViewer.getTable().setSelection(item);
				TableColumn column = tableViewer.getTable().getColumn(index);
				if(null == column) return;
				hitByKey(item.getText(), column.getText(), item.getText(index));
			}
			
		});
	}

	/**
	 * 命中二级索引结果集
	 * @param pkey			一级索引
	 * @param skName		二级索引属性名称
	 * @param keyContext	二级索引内容
	 */
	private void hitByKey(String pkey, String skName, String keyContext) {
		
		if(LOG.isDebugEnabled()) {
			LOG.debug("primaryKey:" + pkey + " secondaryKeyFieldName: " + skName);
		}
		
		/*String curStoreClazz = DataCurrent.getInstance().curStoreClazz;
		if(null == curStoreClazz) return;
		
		Class<Object> classBy = ReflectUtil.getClassBy(curStoreClazz);
		if(null == classBy) return;
		
		if(null == secondaryEntityTable) return;
		
		Class pkClass = ReflectUtil.getPKClass(classBy);

		skName = skName.substring(0, 1).toLowerCase() + skName.substring(1, skName.length());
		
		Class skClass = ReflectUtil.getSKClass(classBy, skName);

		SecondaryIndex store = DBManager.getInstance().getSKStore(pkClass, classBy, skClass, skName);

		EntityCursor entities = store.subIndex(ReflectUtil.getRealType(skClass, keyContext)).entities();
		
		PageInfo secondaryPageInfo = DataCurrent.getInstance().getSecondaryPageInfo();
		
		int index = 0;
		
		List<Object> result = new ArrayList<Object>();
		
		for (Object o : entities) {
			
			//1.不足
			if(index < secondaryPageInfo.fromInt()) {
				++index;
				continue;
			}
			
			//2.命中 
			if(index >= secondaryPageInfo.fromInt() && index < secondaryPageInfo.toInt()) {
				result.add(o);
				++index;
			}
			
			//3.越界
			if(index >= secondaryPageInfo.toInt()) {
				++index;
				break;
			}
		}
		
		if(LOG.isDebugEnabled()) {
			LOG.debug("size:" + result.size());
		}
		
		entities.close();*/
		
		if(!secondaryEntityTable.isOpen()) {
			secondaryEntityTable.open(pkey, skName, keyContext);
		}
	}
	
	@Override
	public void addListener(ILabelProviderListener arg0) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public boolean isLabelProperty(Object arg0, String arg1) {
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener arg0) {
	}

}
